﻿using System;
using System.Collections.Generic;
using GameEditor.View.Components;
using GameEngine.View;
using PureMVC.Interfaces;
using GameEngine.Model.Data;
using GameEngine;
using GameEngine.Model;

namespace GameEditor.View
{
    public class WorldPropertiesEditor_MenuMediator<ItemVOId> : MenuMediator<IWorldEditorMenuViewConstants.ButtonTags, IWorldEditorMenuView<ItemVOId>>
        where ItemVOId : IComparable<ItemVOId>
    {
        IWorldDataProxy<ItemVOId> worldDataProxy;

        public WorldPropertiesEditor_MenuMediator(
            string name,
            IWorldEditorMenuView<ItemVOId> view,
            string outCommand, IWorldDataProxy<ItemVOId> worldDataProxy)
            : base(name, view, outCommand)
        {
            this.worldDataProxy = worldDataProxy;
        }

        //public new IWorldEditorMenuView<ItemVOId> ViewComponent
        //{
        //    get { return base.ViewComponent as IWorldEditorMenuView<ItemVOId>; }
        //}

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            IList<String> notifications = base.ListNotificationInterests();

            notifications.Add(worldDataProxy.THINGADDED_NOTIFICATION);
            notifications.Add(worldDataProxy.THINGREMOVED_NOTIFICATION);
            notifications.Add(ModelFacade<ItemVOId>.Instance.Game.THINGTYPEADDED_NOTIFICATION);
            notifications.Add(ModelFacade<ItemVOId>.Instance.Game.THINGTYPEREMOVED_NOTIFICATION);
            return notifications;
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                default:
                    if (notification.Name.Equals(worldDataProxy.THINGADDED_NOTIFICATION))
                    {
                        AddThing(((ThingDataProxy<ItemVOId>)notification.Body).Id);
                    }
                    else if (notification.Name.Equals(worldDataProxy.THINGREMOVED_NOTIFICATION))
                    {
                        RemoveThing(((ThingDataProxy<ItemVOId>)notification.Body).Id);
                    } else if(notification.Name.Equals(ModelFacade<ItemVOId>.Instance.Game.THINGTYPEADDED_NOTIFICATION)){
                        AddThingType(((ThingTypeDataProxy<ItemVOId>)notification.Body).Id);
                    }else if(notification.Name.Equals(ModelFacade<ItemVOId>.Instance.Game.THINGTYPEREMOVED_NOTIFICATION)){
                        RemoveThingType((ThingTypeVOId)notification.Body);
                    } 
                    break;
            }
            base.HandleNotification(notification);
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            //ViewComponent.SetWorldMenu(worldMenuMediator.ViewComponent);
            //ViewComponent.SetMapMenu(tileMapMenuMediator.ViewComponent);
            //ViewComponent.SetMapView(worldMediator.ViewComponent);
            //ViewComponent.SetItemEditorView(itemEditorMediator.ViewComponent);

            //ViewComponent.MaxMapHeight = proxy.MaxHeight;
            //ViewComponent.MinMapHeight = proxy.MinHeight;
            //ViewComponent.TileSize = proxy.TileSize;
            //ViewComponent.TileHeightUnit = proxy.TileHeightUnit;

            //itemMenuMediator.

            //ViewComponent.OnSizeChanged += new Action<Point>(ViewComponent_OnSizeChanged);
        }

        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
            //Facade.RemoveMediator(mapMediator.MediatorName);
            //Facade.RemoveMediator(Menu.MediatorName);
           // Facade.RemoveMediator(MenuBar.MediatorName);
        }

        #endregion

        public string WorldName
        {
            get { return ViewComponent.WorldName; }
            set { ViewComponent.WorldName = value; }
        }

        public IWorldVOId WorldId
        {
            get;
            set;
        }
       
        //protected virtual void ViewComponent_OnSizeChanged(Point size)
        //{
        //    worldMediator.mapMediator.SetViewSize(size.X, size.Y);
        //}
        public void AddThingType(IThingTypeVOId id) { ViewComponent.AddThingType(id); }
        public void RemoveThingType(IThingTypeVOId id) { ViewComponent.RemoveThingType(id); }
        public List<IThingTypeVOId> GetThingTypes() { return ViewComponent.GetThingTypes(); }
        public IThingTypeVOId SelectedThingType { get { return ViewComponent.SelectedThingType; } }
        public String NewThingName { get { return ViewComponent.NewThingName; } }

        public void AddThing(IThingVOId<ItemVOId> id) { ViewComponent.AddThing(id); }
        public void RemoveThing(IThingVOId<ItemVOId> id) { ViewComponent.RemoveThing(id); }
        public List<IThingVOId<ItemVOId>> GetThings() { return ViewComponent.GetThings(); }
        public IThingVOId<ItemVOId> SelectedThing { get { return ViewComponent.SelectedThing; } }
    }
}